package Operation;

import controller.DataAccessHandler;
import model.pojo.Account;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * 根据关键字查询账户
 */
public class SearchByKeywordIOperation implements IOperation {
    @Override
    public void operate() throws SQLException {
        System.out.println("<按关键字查询账户>");
        //1. 从键盘读取关键字
        Scanner keyboardInput = new Scanner(System.in);
        System.out.print("> ");
        String keyword = keyboardInput.nextLine();

        //2. 访问数据库，执行sql语句
        String sql = "select account_id as accountId,keyword,account_number as accountNumber," +
                "password,remark,creation_time as creationTime,last_modification_time as lastModificationTime " +
                "from tb_accounts " +
                "where keyword like ?";
        //关键字前后拼接上通配符%
        keyword = "%" + keyword + "%";
        List<Account> accounts = DataAccessHandler.query(Account.class, sql, keyword);

        //3. 打印查询结果
        if(accounts != null && accounts.size() != 0){
            for (Account account : accounts){
                account.printAccountInfo();
            }
        }else{
            System.out.println("No account found!");
        }
    }
}
